/**
 * 
 */
package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.constant.ApiPath;
import com.py.credit.entity.Dept;
import com.py.credit.service.DeptService;
import com.py.framework.core.entity.SysOfficeEntity;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.http.HttpAgent;

/**
 * @author dd
 *
 */
@Service
public class DeptServiceImpl implements DeptService {

	private static final Logger LOGGER = LoggerFactory.getLogger(DeptServiceImpl.class);

	@Autowired
	private UrlHelper urlHelper;

	@Override
	public List<Dept> getTopLevelDept() {

		List<Dept> list = new ArrayList<Dept>();

		List<SysOfficeEntity> offices = this.getRootOffices();

		if (offices != null) {

			for (SysOfficeEntity office : offices) {

				Dept dept = new Dept(office.getId(), office.getOfficeCode(), office.getOfficeName(),
						office.getParentId(), null);

				list.add(dept);

			}

		}

		return list;

	}

	@Override
	public List<SysOfficeEntity> getRootOffices() {

		String url = urlHelper.getBaseServerUrl() + ApiPath.DEPT_GET_ROOT_OFFICES;///auth/getRootOffices.do
		//http://10.10.136.199:8080/credit-framework-service/auth/getRootOffices.do

		String sendHttpPost = HttpAgent.getInstance().sendHttpPost(url, "");

		RestServiceResult<List<SysOfficeEntity>> rss = JSON.parseObject(sendHttpPost,
				new TypeReference<RestServiceResult<List<SysOfficeEntity>>>() {
				});
		//"data":[{"id":1,"createTime":1532313196000,"updateTime":1535958345000,"delFlag":1,"officeName":"发改委","officeCode":"gsj","parentId":0,"allParentId":null,"areaId":1,"orderBy":1,"areaName":null,"parentOfficeName":null}
         //List entity.SysOfficeEntity
		return rss.getData();

	}

	@Override
	public SysOfficeEntity getDeptById(String officeId) {

		String url = urlHelper.getBaseServerUrl() + ApiPath.DEPT_GET_OFFICE_BY_ID;

		Map<String, String> map = new HashMap<String, String>();

		map.put("officeId", officeId);

		String sendHttpPost = HttpAgent.getInstance().sendHttpPost(url, map, "");

		RestServiceResult<SysOfficeEntity> rss = JSON.parseObject(sendHttpPost,
				new TypeReference<RestServiceResult<SysOfficeEntity>>() {
				});

		return rss.getData();

	}

}
